//
//  AlarmRecordViewController.m
//  LocomotiveGuards
//
//  Created by xys on 15/11/30.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "AlarmRecordViewController.h"
#import "UIImageView+WebCache.h"
#import "ThanksViewController.h"
#import "PopLoadMsg.h"
#import "PopMsg.h"

@interface AlarmRecordViewController ()
{
    UIView *highBackView;
    UIView *lowBackView;
    UIImageView *bicycleimage;
    UILabel *lab_deviceNum;
    UILabel *lab_brand;
    UILabel *paichusuo;
    BOOL start;
    BOOL end;
    
    
    // 添加一键取消报警按钮
    UIButton *cancleBtn;
}

@end

@implementation AlarmRecordViewController
@synthesize time,psbtext,eportStatus;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.rightbtn removeFromSuperview];
    [self.centerbtn setImage:nil forState:UIControlStateNormal];
    [self.centerbtn setTitle:@"报警备案" forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.leftbtn addTarget:self action:@selector(goBackButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    

    
    [self configView];
    [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"yiwan"];
  
    [self dorequestForgetEbicycleInfo];
}

-(void)configView{
    highBackView=[[UIView alloc]initWithFrame:CGRectMake(0, 20+44*H, SCREEN_WIDTH, SCREEN_HEIGHT/2-20-44*H-30*H)];
    
    //SCREEN_HEIGHT/2+40*H
    
    
    
    
    highBackView.backgroundColor=[UIColor colorWithRed:(CGFloat)40/255 green:(CGFloat)152/255 blue:(CGFloat)250/255 alpha:1.0];
    [self.view addSubview:highBackView];
    int imgW=75*H;
    UIImageView *renderimage=[[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-imgW-10)/2, 10*H, imgW+10, imgW+10)];
    renderimage.image=[UIImage imageNamed:@"image_top_carbg"];
    [highBackView addSubview:renderimage];//圆形的背景图
    
    
    bicycleimage=[[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-imgW-10)/2+5,10*H+5, imgW, imgW)];
    bicycleimage.image=[UIImage imageNamed:@"image_top_car"];//被圆形化的图片
    bicycleimage.layer.masksToBounds=YES;
    bicycleimage.layer.cornerRadius=imgW/2;
    [highBackView addSubview:bicycleimage];
    
    lab_deviceNum=[[UILabel alloc]initWithFrame:CGRectMake(0, 10*H+10+imgW+10*H, SCREEN_WIDTH, 20*H)];
    lab_deviceNum.textAlignment=NSTextAlignmentCenter;
    lab_deviceNum.text=@"设备号？";
    lab_deviceNum.font=[UIFont systemFontOfSize:13];
    lab_deviceNum.textColor=[UIColor whiteColor];
    [highBackView addSubview:lab_deviceNum];
    
    lab_brand=[[UILabel alloc]initWithFrame:CGRectMake(0, 10*H+10+imgW+35*H, SCREEN_WIDTH, 20*H)];
    lab_brand.textAlignment=NSTextAlignmentCenter;
    lab_brand.text=@"品牌？";
    lab_brand.font=[UIFont systemFontOfSize:13];
    lab_brand.textColor=[UIColor whiteColor];
    [highBackView addSubview:lab_brand];
    
    paichusuo=[[UILabel alloc]initWithFrame:CGRectMake(0, 10*H+10+imgW+60*H, SCREEN_WIDTH, 20*H)];
    paichusuo.textAlignment=NSTextAlignmentCenter;
    paichusuo.text=psbtext;
    paichusuo.font=[UIFont systemFontOfSize:13];
    paichusuo.textColor=[UIColor whiteColor];
    [highBackView addSubview:paichusuo];
    
    
    
    UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(10*W, SCREEN_HEIGHT/2+10*H-30*H, 80*W, 20*H)];
    lab1.text=@"案件当前状态";
    lab1.font=[UIFont systemFontOfSize:12];
    lab1.textColor=[UIColor blackColor];
    [self.view addSubview:lab1];
    UILabel *lab2=[[UILabel alloc]initWithFrame:CGRectMake(90*W, SCREEN_HEIGHT/2+10*H-30*H, 80*W, 20*H)];
    lab2.textAlignment=NSTextAlignmentCenter;
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:time.doubleValue/1000];
    
    
    lab2.text=[formatter stringFromDate:date];
    lab2.font=[UIFont systemFontOfSize:12];
    lab2.textColor=[UIColor blackColor];
    [self.view addSubview:lab2];
    
    UILabel *lab3=[[UILabel alloc]initWithFrame:CGRectMake(180*W,SCREEN_HEIGHT/2+10*H-30*H, 50*W, 20*H)];
    lab3.font=[UIFont systemFontOfSize:12];
    lab3.textColor=[UIColor blackColor];
    [self.view addSubview:lab3];
    
    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(240*W,SCREEN_HEIGHT/2+10*H-30*H, 70*W, 20*H)];
    btn1.backgroundColor=[UIColor clearColor];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.titleLabel.font=[UIFont systemFontOfSize:12];
    btn1.enabled=NO;
    [self.view addSubview:btn1];
    
    lowBackView=[[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/2+40*H-40*H, SCREEN_WIDTH, 150*H)];
//    lowBackView.backgroundColor = [UIColor redColor];
    [self.view addSubview:lowBackView];
    
    UIImageView *image_progress=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60*H, SCREEN_WIDTH, 45)];
    image_progress.image=[UIImage imageNamed:@"putonrecods_content"];
    [lowBackView addSubview:image_progress];
    
    UILabel *labb1=[[UILabel alloc]initWithFrame:CGRectMake(38*W, 28, 21*W, 15)];
    labb1.text=@"01";
    labb1.textAlignment=NSTextAlignmentCenter;
    labb1.font=[UIFont systemFontOfSize:12];
    labb1.textColor=[UIColor blackColor];
    [image_progress addSubview:labb1];
    
    UILabel *labb2=[[UILabel alloc]initWithFrame:CGRectMake(94*W, 0, 21*W, 15)];
    labb2.text=@"02";
    labb2.textAlignment=NSTextAlignmentCenter;
    labb2.font=[UIFont systemFontOfSize:12];
    labb2.textColor=[UIColor blackColor];
    [image_progress addSubview:labb2];
    
    UILabel *labb3=[[UILabel alloc]initWithFrame:CGRectMake(152*W, 28, 21*W, 15)];
    labb3.text=@"03";
    labb3.textAlignment=NSTextAlignmentCenter;
    labb3.font=[UIFont systemFontOfSize:12];
    labb3.textColor=[UIColor blackColor];
    [image_progress addSubview:labb3];
    
    UILabel *labb4=[[UILabel alloc]initWithFrame:CGRectMake(208*W, 0, 21*W, 15)];
    labb4.text=@"04";
    labb4.textAlignment=NSTextAlignmentCenter;
    labb4.font=[UIFont systemFontOfSize:12];
    labb4.textColor=[UIColor blackColor];
    [image_progress addSubview:labb4];
    
    UILabel *labb5=[[UILabel alloc]initWithFrame:CGRectMake(265*W, 28, 21*W, 15)];
    labb5.text=@"05";
    labb5.textAlignment=NSTextAlignmentCenter;
    labb5.font=[UIFont systemFontOfSize:12];
    labb5.textColor=[UIColor blackColor];
    [image_progress addSubview:labb5];
    
    UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(26*W, 15*H, 35*W, 35*W)];

    image1.image=[UIImage imageNamed:@"6pic_icon02_ing"];
    [lowBackView addSubview:image1];
    
    UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(85*W, 60*H+45, 35*W, 35*W)];
    
    image2.image=[UIImage imageNamed:@"6pic_icon02_ing"];
    [lowBackView addSubview:image2];
    
    UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(140*W, 15*H, 35*W, 35*W)];
    
    image3.image=[UIImage imageNamed:@"6pic_icon02_ing"];
    [lowBackView addSubview:image3];

    UIImageView *image4=[[UIImageView alloc]initWithFrame:CGRectMake(200*W, 60*H+45, 35*W, 35*W)];
    
    image4.image=[UIImage imageNamed:@"6pic_icon02_ing"];
    [lowBackView addSubview:image4];
    
    UIImageView *image5=[[UIImageView alloc]initWithFrame:CGRectMake(255*W, 15*H, 35*W, 35*W)];
    
    image5.image=[UIImage imageNamed:@"6pic_icon02_ing"];
    [lowBackView addSubview:image5];

    //eportStatus=@"5";
    switch (eportStatus.integerValue) {
        case 1:
            lab3.text=@"案件审核";
            [btn1 setTitle:@"处理中" forState:UIControlStateNormal];
            image1.image=[UIImage imageNamed:@"6pic_icon01_ing"];
            image2.image=[UIImage imageNamed:@"6pic_icon02_before"];
            image3.image=[UIImage imageNamed:@"6pic_icon03_before"];
            image4.image=[UIImage imageNamed:@"6pic_icon04_before"];
            image5.image=[UIImage imageNamed:@"6pic_icon05_before"];
            break;
        case 2:
            lab3.text=@"受理案件";
            [btn1 setTitle:@"案件受理中" forState:UIControlStateNormal];
            image1.image=[UIImage imageNamed:@"6pic_icon01_finish"];
            image2.image=[UIImage imageNamed:@"6pic_icon02_ing"];
            image3.image=[UIImage imageNamed:@"6pic_icon03_before"];
            image4.image=[UIImage imageNamed:@"6pic_icon04_before"];
            image5.image=[UIImage imageNamed:@"6pic_icon05_before"];

            break;
        case 3:
            lab3.text=@"案件侦破";
            [btn1 setTitle:@"侦破中" forState:UIControlStateNormal];
            image1.image=[UIImage imageNamed:@"6pic_icon01_finish"];
            image2.image=[UIImage imageNamed:@"6pic_icon02_finish"];
            image3.image=[UIImage imageNamed:@"6pic_icon03_ing"];
            image4.image=[UIImage imageNamed:@"6pic_icon04_before"];
            image5.image=[UIImage imageNamed:@"6pic_icon05_before"];
            break;
        case 4:
            lab3.text=@"出警寻车";
            [btn1 setTitle:@"出警寻车" forState:UIControlStateNormal];
            image1.image=[UIImage imageNamed:@"6pic_icon01_finish"];
            image2.image=[UIImage imageNamed:@"6pic_icon02_finish"];
            image3.image=[UIImage imageNamed:@"6pic_icon03_finish"];
            image4.image=[UIImage imageNamed:@"6pic_icon04_ing"];
            image5.image=[UIImage imageNamed:@"6pic_icon05_before"];
            break;
        case 5:
            lab3.text=@"返还失车";
            [btn1 setTitle:@"点击答谢" forState:UIControlStateNormal];
            btn1.enabled=YES;
            [btn1 setTitleColor:[UIColor colorWithRed:(CGFloat)21/255 green:(CGFloat)124/255 blue:(CGFloat)244/255 alpha:1.0] forState:UIControlStateNormal];
            [btn1 addTarget:self action:@selector(tapped_zan) forControlEvents:UIControlEventTouchUpInside];
            image1.image=[UIImage imageNamed:@"6pic_icon01_finish"];
            image2.image=[UIImage imageNamed:@"6pic_icon02_finish"];
            image3.image=[UIImage imageNamed:@"6pic_icon03_finish"];
            image4.image=[UIImage imageNamed:@"6pic_icon04_finish"];
            image5.image=[UIImage imageNamed:@"6pic_icon05_ing"];
            break;
            
        default:
            break;
    }
    
    
    
    cancleBtn  = [[UIButton alloc]initWithFrame:CGRectMake(40*W, lowBackView.frame.origin.y+lowBackView.frame.size.height + 16*H, SCREEN_WIDTH-40*W*2, 40*H)];
//    cancleBtn.backgroundColor = [UIColor greenColor];
    
    cancleBtn.backgroundColor = [UIColor colorWithRed:206.0/255 green:67.0/255 blue:67.0/255 alpha:1];
    [self.view addSubview:cancleBtn];
    [cancleBtn setTitle:@"取消报警" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancleBtn.layer.masksToBounds = YES;
    cancleBtn.layer.cornerRadius = 6.0f;
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18*H];
    
    //    [cancleBtn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];
    //    [cancleBtn setImage:[UIImage imageNamed:@"ph2"] forState:UIControlStateSelected];
    
    
    //进行一键取消报警按钮操作
    [cancleBtn addTarget:self action:@selector(cancle:) forControlEvents:UIControlEventTouchUpInside];
    
    
}


#pragma mark取消报警操作
-(void)cancle:(id)sender

{

//发送网络请求进行取消报警
// 取消后再次进行网络请求，获取最新的网络状态按钮的状态消失不让被点击
//    cancleBtn.selected = YES;
//    cancleBtn.selected = NO;
//还得判断状态是否是被取消了
  
    
    NSString *urlStr = [NSString stringWithFormat:@"%@getLostReport.do",urlstring];
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
      NSDictionary * paramDict = @{@"loginId":loginId,@"bid":[[NSUserDefaults standardUserDefaults]objectForKey:@"bid"]};
    
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlStr withParam:paramDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSNumber * flag = [responseObject objectForKey:@"flag"];
        if (flag.longValue == 0 )
            
        {
            cancleBtn.userInteractionEnabled = NO;
            cancleBtn.alpha = 0.4;
        
        
        
        }
            
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        
        NSLog(@"网络问题不能获取订单取消状态");
        
    }];
    
    





}


#pragma mark- 网络请求

- (void)getEbicycleInfo
{
    NSString * urlMethod = [NSString stringWithFormat:@"%@getLostReport.do",urlstring];
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
    NSDictionary * paramDict = @{@"loginId":loginId,@"bid":[[NSUserDefaults standardUserDefaults]objectForKey:@"bid"]};
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSDictionary * responseDict = responseObject;
         
         NSNumber * flag = [responseDict objectForKey:@"flag"];
         if (flag.longValue == 0 )
         {
             NSString *pro;
             NSString *cit;
             NSString *psb;

             NSString *province=[responseDict objectForKey:@"province"];
             if(province){
                 pro=[province stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             }
             NSString *city=[responseDict objectForKey:@"city"];
             if(city){
                  cit=[city stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             }
             NSString *psbname=[responseDict objectForKey:@"psbName"];
             if(psbname){
                  psb=[psbname stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             }
             paichusuo.text=[NSString stringWithFormat:@"%@%@%@",pro,cit,psb];
             
             //NSString *reportStatus
             
         }
         else
         {
             
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"查询防护状态失败！");
     }];
}


-(void)dorequestForgetEbicycleInfo{
    start=YES;
    end=YES;
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            if(start==YES){
                [loadmsg loadImages];
            }
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime+endloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            end=NO;
            if(start==NO){
                [loadmsg.imageview removeFromSuperview];
            }
        });
    });
    
    NSString * urlMethod = [NSString stringWithFormat:@"%@getEbicycleInfo.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":[[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"]};
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSDictionary * responseDict = responseObject;
         start=NO;
         
         if(end==NO){
             [loadmsg.imageview removeFromSuperview];
         }

         NSNumber * flag = [responseDict objectForKey:@"flag"];
         if (flag.longValue == 0 )
         {
             //设置报警备案按钮可用
             
             NSArray *array=[responseDict objectForKey:@"bicycleLists"];
             NSDictionary *dic=array[0];
             if(dic){
                 NSString *picurl=[dic objectForKey:@"picUrl"];
                 NSString *newurl=[NSString stringWithFormat:@"https://appif.jszhtech.com:8443/ebappif/upImg/%@",picurl];
                 [bicycleimage sd_setImageWithURL:[NSURL URLWithString:newurl] placeholderImage:[UIImage imageNamed:@"image_top_car"]];
                 
                 NSString *simID=[dic objectForKey:@"simId"];
                 lab_deviceNum.text=[NSString stringWithFormat:@"设备号 %@",simID];
                 
                 NSString *brand=[dic objectForKey:@"brand"];
                 lab_brand.text=[NSString stringWithFormat:@"品牌%@",brand];
             }
             
         }
         else
         {
             
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"查询防护状态失败！");
         start=NO;
          [PopMsg showMessage:@"网络出错" Lasttime:1.0f];
         if(end==NO){
             [loadmsg.imageview removeFromSuperview];
         }

     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    NSDictionary *dic=@{@"key":@"2"};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changetabbartwo" object:nil userInfo:dic];

}
#pragma mark - button clicled handler -


-(void)tapped_zan{
    ThanksViewController *new=[ThanksViewController new];
    [self.navigationController pushViewController:new animated:YES];
    NSLog(@"zan");
}

-(void)viewDidDisappear:(BOOL)animated{
    NSString *yiwan=[[NSUserDefaults standardUserDefaults]objectForKey:@"yiwan"];
    if([yiwan isEqualToString:@"1"]){
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}

- (void)goBackButtonClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
